package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketKeyword;
import com.cskaoyan.bean.vo.KeywordListVO;
import com.cskaoyan.service.KeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商场管理-关键词
 * 创建日期: 2022/04/07 23:22
 *
 * @author ChengWenqi
 */

@RestController
@RequestMapping("admin/keyword")
public class MarketKeywordController {

    @Autowired
    KeywordService keywordService;

    // 所有关键词 admin/keyword/list
    @RequestMapping("list")
    public BaseRespVo keywordList(BaseParam param, String keyword, String url) {
        KeywordListVO keywordListVO = keywordService.queryKeywordList(param, keyword, url);
        return BaseRespVo.ok(keywordListVO);
    }

    // 添加关键词 admin/keyword/create
    // 参数校验：关键词和链接notBlank
    @RequestMapping("create")
    public BaseRespVo keywordCreate(@RequestBody @Validated MarketKeyword keyword) {
        MarketKeyword marketKeyword = keywordService.createKeyword(keyword);
        return BaseRespVo.ok(marketKeyword);
    }

    // 更新关键词 admin/keyword/update
    // 参数校验：关键词和链接notBlank
    @RequestMapping("update")
    public BaseRespVo keywordUpdate(@RequestBody @Validated MarketKeyword keyword) {
        MarketKeyword newKeyword = keywordService.keywordUpdate(keyword);
        // 规定：如果根据id没找到记录，返回null
        if (newKeyword == null) {
            return BaseRespVo.fail(10000, "关键词记录不存在！");
        }
        return BaseRespVo.ok(newKeyword);
    }

    // 删除关键词 admin/keyword/delete
    // 逻辑删除
    @RequestMapping("delete")
    public BaseRespVo keywordDelete(@RequestBody @Validated MarketKeyword keyword) {
        int code = keywordService.keywordDelete(keyword);
        if (code == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail(404, "要删除的记录不存在！");
    }
}
